home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 212 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  58 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: DJS
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: help on syntax
  5. Date: Wed, 03 Jan 1996 04:35:29 GMT
  6. Organization: n/a
  7. Message-ID: <4cd0up$bst@dub-news-svc-6.compuserve.com>
  8. References: <96002.222728APCCU@CUNYVM.CUNY.EDU>
  9. NNTP-Posting-Host: ad73-161.compuserve.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Paul Abrilla <APCCU@CUNYVM.CUNY.EDU> wrote:
  13.  
  14. >Hi,
  15. >I'm trying to run a simple program, but I'm getting a 'misplaced
  16. >else' error everytime I compile it.  However, when I tried to modify
  17. >the program the if statement it ran perfectly fine.  Can anyone please
  18. >tell me what's going on.  Program I is the original, and Program II
  19. >is the modified version.  Any help will be appreciated.  Please send
  20. >replies directly to my account.  Thanks.
  21. >PROGRAM I
  22. >#include <iostream.h>
  23. >void main()
  24. >{
  25. > //...
  26. >      if (c = (a-b))
  27. >          cout << "a: ";
  28. >          cout << a;
  29. >          cout << " minus b: ";
  30. >          cout << b;
  31. >          cout << " quals c: ";
  32. >          cout << c << endl;
  33. >      else
  34. >          cout << "a-b does not equal c: " << endl;
  35. >}
  36.  
  37. The problem is that you have more than one statement after the if
  38. but before the else. If you want multiple statements to run within an
  39. if block, enclose them in braces:
  40.  
  41. if (someExpression) {
  42.     statementOne();
  43.     statementTwo();
  44.     statementThree();
  45. }
  46. else {
  47.     // more statements, as many as you like
  48. }
  49.  
  50. regards,
  51. David
  52.  
  53.